package com.pks.datacenter.dto;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 菜单
 * 
 * @author zouchongjin@sina.com
 * @data 2015年2月27日
 */
public class MenuDto {

	private Long id;// 用于生成树形结构
	private Long pid;// 用于生成树形结构

	private String url;// 左侧菜单/顶部子应用 --链接地址

	private String title;// 顶部子应用--名称
	private String imgUrl;// 顶部子应用--图标地址
	private String iconfont;	
	private String name;// 左侧菜单--名称
	private Boolean open;// 左侧菜单--是否自动展开
	private List<MenuDto> childs;// 左侧菜单--子菜单

	public MenuDto() {
		super();
	}
	
	public static MenuDto initPMenuDto(String name, MenuDto... sub) {
		return initPMenuDto(name, false, sub);
	}
	
	public static MenuDto initPMenuDto(String name, boolean open, MenuDto... sub) {
		List<MenuDto> list = new LinkedList<MenuDto>();
		for (MenuDto m : sub) {
			list.add(m);
		}
		return new MenuDto(name, "#", open, list);
	}
	
	public MenuDto(Long id, Long pid, String url, String name,String iconfont) {
		super();
		this.id = id;
		this.pid = pid;
		this.url = url;
		this.name = name;
		this.iconfont = iconfont;
		this.open = false;
	}

	/** 左侧菜单 */
	public MenuDto(String name, String url) {
		super();
		this.url = url;
		this.name = name;
	}

	/** 左侧菜单 */
	public MenuDto(String name, String url, List<MenuDto> childs) {
		super();
		this.name = name;
		this.url = url;
		this.childs = childs;
	}

	/** 左侧菜单 */
	public MenuDto(String name, String url, Boolean open, List<MenuDto> childs) {
		super();
		this.name = name;
		this.url = url;
		this.open = open;
		this.childs = childs;
	}

	/** 顶部子应用 */
	public MenuDto(String title, String imgUrl, String url) {
		super();
		this.title = title;
		this.imgUrl = imgUrl;
		this.url = url;
	}

	/** 用于Catalog和Module转换 */
	public MenuDto(Long id, Long pid, String url, String name) {
		super();
		this.id = id;
		this.pid = pid;
		this.url = url;
		this.name = name;
		this.open = false;
	}

	/** 集合数据转换为树形数据，并且设置父菜单的url值为"#" */
	public static List<MenuDto> convertTree(List<MenuDto> clist) {
		List<MenuDto> result = new ArrayList<MenuDto>();
		if (clist != null && clist.size() > 0) {
			for (MenuDto c : clist) {
				if (c.getPid() == null) {
					addChilds(c, clist);
					result.add(c);
				}
			}
		}
		return result;
	}

	private static void addChilds(MenuDto menu, List<MenuDto> menuList) {
		if (menu != null && menuList != null) {
			for (MenuDto c : menuList) {
				if (c.getPid() != null && c.getPid().equals(menu.getId())) {// 如果c是menu的子菜单
					List<MenuDto> chi = menu.getChilds();
					if (chi == null) {
						chi = new ArrayList<MenuDto>();
					}
					addChilds(c, menuList);
					chi.add(c);
					menu.setChilds(chi);
					menu.setUrl("#");
				}
			}
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public String getTitle() {
		return title;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Boolean getOpen() {
		return open;
	}

	public void setOpen(Boolean open) {
		this.open = open;
	}

	public List<MenuDto> getChilds() {
		return childs;
	}

	public void setChilds(List<MenuDto> childs) {
		this.childs = childs;
	}

	public String getIconfont() {
		return iconfont;
	}

	public void setIconfont(String iconfont) {
		this.iconfont = iconfont;
	}
	
}
